package com.tt.aile.item.controller;

import com.tt.aile.common.pojo.PageResult;
import com.tt.aile.item.pojo.Brand;
import com.tt.aile.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 86176 shkstrat
 * @date 2020/9/21 - 18:02
 * 分页查询品牌
 * key,
 * page,
 * rows,
 * sortBy,
 * desc
 */

@Controller
@RequestMapping("brand")
public class BrandController {

    @Autowired
    BrandService brandService;

    //查询品牌
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> queryBrandByPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "sortBy",required = false) String sortBy,
            @RequestParam(value = "desc",required = false) Boolean desc

    ){
        PageResult<Brand> result = this.brandService.queryBrandByPage(key,page,rows,sortBy,desc);
        if(CollectionUtils.isEmpty(result.getItems())){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(result);

    }

    @PostMapping
    public ResponseEntity<Void> saveBrand(Brand brand, @RequestParam("cids") List<Long> cids){

//        System.out.println(brand);
        this.brandService.saveBrand(brand,cids);
//        System.out.println("------------------"+cids.toString());
//        return "ss";
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCid(@PathVariable("cid") Long cid){

        List<Brand> brands = this.brandService.queryBrandByCid(cid);
        if (CollectionUtils.isEmpty(brands)){

            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }

        return ResponseEntity.ok(brands);
    }

    @ResponseBody
    @RequestMapping("test")
    public String test(){
        return "sss";
    }


    @ResponseBody
    @GetMapping("/{id}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("id") Long id){
        //System.out.println("quebrangbyId------------------");
        Brand brand = this.brandService.queryBrandById(id);
        if (brand==null){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(brand);
    }
}
